﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Portal.Nhibernate.Repository;

namespace Portal.Web.Areas.Admin.Controllers
{
    public class CandidatesController : BaseAdminController
    {
        private readonly ICandidatesRepository<ICandidates> _candidatesRepository;

        public CandidatesController()
        {
            _candidatesRepository = new CandidatesRepository();
        }
        public CandidatesController(ICandidatesRepository<ICandidates> candidatesRepository)
        {
            _candidatesRepository = candidatesRepository;
        }

        // GET: /Admin/Candidates/

        public ActionResult Index()
        {
            _candidatesRepository.Get();
            return View();
        }

        public ActionResult GetCandidatesJson(string sort = "CreateTime", string order = "DESC", int page = 1, int rows = 10)
        {
            return Json(_candidatesRepository.Get().Sorting(sort, order).JqEasyuiDataGrid(page, rows));
        }
        public ActionResult GetListJson(string sort = "CreateTime", string order = "DESC", int page = 1, int rows = 10)
        {
            return Json(_candidatesRepository.Get().Sorting(sort, order).JqEasyuiDataGrid(page, rows));
        }
        public ActionResult Create()
        {
            var model = _candidatesRepository.CreateEntity();
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]

        public ActionResult Create(FormCollection formCollection, Candidates entity)
        {
            var model = _candidatesRepository.CreateEntity();
            model.Culture = Culture;
            model.Founder = CurrentUserName;
            model.Name = entity.Name;


            _candidatesRepository.Add(model);
            _candidatesRepository.Save();
            ViewData["Result"] = new AjaxResult { message = "添加成功", state = false };
            return View(model);
        }

        public ActionResult Edit(string id)
        {
            var model = _candidatesRepository.Get().SingleOrDefault(o => o.Id == id);
            if (model == null)
                return NotFount(new AjaxResult { message = "没有找到记录", state = false });
            return View(model);
        }
        [HttpPost]

        public ActionResult Edit(Candidates entity)
        {
            var model = _candidatesRepository.Get().SingleOrDefault(o => o.Id == entity.Id);
            if (model == null)
                return NotFount(new AjaxResult { message = "没有找到记录", state = false });
            model.Culture = Culture;
            model.Founder = CurrentUserName;
            model.Name = entity.Name;
            _candidatesRepository.Update(model);
            _candidatesRepository.Save();
            ViewData["Result"] = new AjaxResult { message = "修改成功", state = false };
            return View(model);
        }
        [HttpPost]
        public ActionResult Delete(List<string> id, FormCollection collection)
        {
            var list = _candidatesRepository.Get().Where(o => id.Contains(o.Id));
            list.ForEach(o => _candidatesRepository.Delete(o));
            _candidatesRepository.Save();
            return Json(new AjaxResult { state = true, message = "删除成功" });
        }
        public ActionResult Details(string id)
        {
            var model = _candidatesRepository.Get().SingleOrDefault(o => o.Id == id);
            if (model == null)
                return NotFount(new AjaxResult { message = "没有找到记录", state = false });
            if (Request.IsAjaxRequest())
                return View("DetailsControl", model);
            return View(model);
        }
        public ActionResult DetailsControl(string id)
        {
            var model = _candidatesRepository.Get().SingleOrDefault(o => o.Id == id);
            if (model == null)
                return NotFount(new AjaxResult { message = "没有找到记录", state = false });
            return View(model);
        }

        [HttpPost]
        public ActionResult Read(string id, FormCollection collection)
        {
            var model = _candidatesRepository.Get().SingleOrDefault(o => o.Id == id);
            if (model != null) model.IsRead = true;
            else
            {
                return Json(new AjaxResult { state = false, message = "没有找到记录" });
            }
            _candidatesRepository.Save();
            return Json(new AjaxResult { state = true, message = "设置成功" });
        }
    }
}
